//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "uFormatNombre.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CComboBx"
#pragma link "CEdit"
#pragma link "CEditNum"
#pragma link "CCheckB"
#pragma link "CBitBtn"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
void __fastcall TfrmFormatNombre::InitNumber( )
{
    m_Init = true;
    if( m_pNumFormat && !::IsBadReadPtr(m_pNumFormat, sizeof(*m_pNumFormat)))
    {
        cbxFmtNegNum->ItemIndex     = m_pNumFormat->NegativeOrder;
        cbxDecimal->Text            = m_pNumFormat->DecimalSep;
        cbxLeadingZero->ItemIndex   = m_pNumFormat->LeadingZero;
        if( m_pNumFormat->ThousandSep == "\xA0")
            cbxChiffreGroupe->ItemIndex = 0;
        else if( m_pNumFormat->ThousandSep == " ")
            cbxChiffreGroupe->ItemIndex = 1;
        else cbxChiffreGroupe->Text = m_pNumFormat->ThousandSep;
        edNbChiffreGroupe->Valeur   = m_pNumFormat->Grouping;
        ednNbDecimale->Valeur       = m_pNumFormat->NumDigits;
        ckZeroSignificatif->Checked = m_pNumFormat->ZeroSignificatif;
        ednNbUniteArrondies->Valeur = m_pNumFormat->NbUniteArrondies;
        cbxSignePlus->ItemIndex     = (int)m_pNumFormat->SignePositif;
        ckDecimalZeroSignificatif->Checked = m_pNumFormat->DecimaleZeroSignificatif;
    }
    else if( m_pNumFormatDefaut)
    {
        cbxFmtNegNum->ItemIndex     = m_pNumFormatDefaut->NegativeOrder;
        cbxDecimal->Text            = m_pNumFormatDefaut->DecimalSep;
        cbxLeadingZero->ItemIndex   = m_pNumFormatDefaut->LeadingZero;
        if( m_pNumFormatDefaut->ThousandSep == "\xA0")       cbxChiffreGroupe->ItemIndex = 0;
        else if( m_pNumFormatDefaut->ThousandSep == " ")     cbxChiffreGroupe->ItemIndex = 1;
        else cbxChiffreGroupe->Text = m_pNumFormatDefaut->ThousandSep;
        edNbChiffreGroupe->Valeur   = m_pNumFormatDefaut->Grouping;
        ednNbDecimale->Valeur       = m_pNumFormatDefaut->NumDigits;
        ckZeroSignificatif->Checked = m_pNumFormatDefaut->ZeroSignificatif;
        ednNbUniteArrondies->Valeur = m_pNumFormatDefaut->NbUniteArrondies;
        cbxSignePlus->ItemIndex     = (int)m_pNumFormatDefaut->SignePositif;
        ckDecimalZeroSignificatif->Checked = m_pNumFormatDefaut->DecimaleZeroSignificatif;
    }else
    {
        cbxDecimal->ItemIndex       = 0;
        cbxChiffreGroupe->ItemIndex = 0;
        cbxFmtNegNum->ItemIndex     = 1;
        cbxLeadingZero->ItemIndex   = 1;
        ednNbDecimale->Valeur       = 2;
        cbxSignePlus->ItemIndex     = (int)sp_Aucun;
    }
    m_Init = false;
}
void __fastcall TfrmFormatNombre::InitCurrency( )
{
    m_pNumFormat       = m_pCurFormat;
    m_pNumFormatDefaut = m_pCurFormatDefaut;
    InitNumber( );
    m_pNumFormat       = NULL;
    m_pNumFormatDefaut = NULL;
    m_Init = true;
    if( m_pCurFormat && !::IsBadWritePtr( m_pCurFormat, sizeof(m_pCurFormat)))
    {
      //  ((EICNumberFmt*)m_pCurFormat)->Assign( *m_pNumFormat);
        cbxMonaie->Text           = m_pCurFormat->CurrencySymbol;
        cbxFmtPos->ItemIndex      = m_pCurFormat->PositiveOrder;
        cbxFmtNegCur->ItemIndex   = m_pCurFormat->NegativeOrder;
        m_pNumFormat = NULL;
    }
    else if( m_pCurFormatDefaut)
    {
      //  ((EICNumberFmt*)m_pCurFormat)->Assign( *m_pCurFormatDefaut);
        cbxMonaie->Text           = m_pCurFormatDefaut->CurrencySymbol;
        cbxFmtPos->ItemIndex      = m_pCurFormatDefaut->PositiveOrder;
        cbxFmtNegCur->ItemIndex   = m_pCurFormatDefaut->NegativeOrder;
    }else
    {
        cbxMonaie->ItemIndex        = 0;
        cbxFmtPos->ItemIndex        = 1;
        cbxFmtNegCur->ItemIndex     = 5;
    }
    m_Init = false;
}
//---------------------------------------------------------------------------
__fastcall TfrmFormatNombre::TfrmFormatNombre(TComponent* Owner, EICNumberFmt *p, const EICNumberFmt *pDef)
    : TForm(Owner)
{
    m_pNumFormat = dynamic_cast<EICNumberFmt*>(p);
    m_pNumFormatDefaut = dynamic_cast<const EICNumberFmt*>(pDef);
    m_pCurFormat = NULL;
    m_pCurFormatDefaut = NULL;
    bCurrency = false;
    InitNumber( );
    RefreshApercu(NULL);
    btnParDefaut->Visible = (pDef != NULL);
}
//---------------------------------------------------------------------------
__fastcall TfrmFormatNombre::TfrmFormatNombre(TComponent* Owner, EICCurrencyFmt *p, const EICCurrencyFmt *pDef)
    : TForm(Owner)
{
    bCurrency = true;
    m_pCurFormat = dynamic_cast<EICCurrencyFmt*>(p);
    m_pNumFormat = NULL;
    m_pCurFormatDefaut = dynamic_cast<const EICCurrencyFmt*>(pDef);
    m_pNumFormatDefaut = NULL;

    InitCurrency( );
    RefreshApercu(NULL);
    btnParDefaut->Visible = (pDef != NULL);
    cbxFmtPos->Enabled = true;
    cbxMonaie->Enabled = true;
    LblPositif->Enabled = true;
    LblMonnaie->Enabled = true;
    LblComment->Enabled = true;
    cbxFmtNegCur->Visible = true;
    cbxFmtNegNum->Visible = false;
    cbxFmtNegCur->Left = cbxFmtNegNum->Left;
}
//---------------------------------------------------------------------------
void __fastcall TfrmFormatNombre::RefreshApercu(TObject *Sender)
{
    if( m_Init) return;
    AnsiString sChiffreGroupe;
    switch( cbxChiffreGroupe->ItemIndex)
    {
        case 0  : sChiffreGroupe = "\xA0"; break;
        case 1  : sChiffreGroupe = " "; break;
        default : sChiffreGroupe = cbxChiffreGroupe->Text;
    }
    AnsiString asPos = "0123456.7", asNeg = "-0123456.7";
    EICNumberFmt *nFmt;

    if( bCurrency)
    {
        if( !m_pCurFormat || ::IsBadWritePtr( m_pCurFormat, sizeof(m_pCurFormat)) )
            m_pCurFormat = new EICCurrencyFmt();
        m_pCurFormat->CurrencySymbol    = cbxMonaie->Text;
        m_pCurFormat->PositiveOrder     = cbxFmtPos->ItemIndex;
        m_pCurFormat->NegativeOrder     = cbxFmtNegCur->ItemIndex;
        nFmt = m_pCurFormat;
    }
    else
    {
        if( !m_pNumFormat || ::IsBadWritePtr( m_pNumFormat, sizeof(m_pNumFormat)) )
            m_pNumFormat = new EICNumberFmt();
        m_pNumFormat->NegativeOrder     = cbxFmtNegNum->ItemIndex;
        nFmt = m_pNumFormat;
    }
    nFmt->NbUniteArrondies  = ednNbUniteArrondies->Valeur;
    nFmt->LeadingZero       = cbxLeadingZero->ItemIndex;
    nFmt->DecimalSep        = cbxDecimal->Text;
    nFmt->NumDigits         = ednNbDecimale->Valeur;
    nFmt->ThousandSep       = sChiffreGroupe;
    nFmt->Grouping          = edNbChiffreGroupe->Valeur;
    nFmt->ZeroSignificatif  = ckZeroSignificatif->Checked;
    nFmt->SignePositif      = (eSignePositif)cbxSignePlus->ItemIndex;
    nFmt->DecimaleZeroSignificatif  = ckDecimalZeroSignificatif->Checked;
    Text_edPositif->Text = nFmt->Chaine( asPos);
    Text_edNegatif->Text = nFmt->Chaine( asNeg);
}
//---------------------------------------------------------------------------

void __fastcall TfrmFormatNombre::cmdOKClick(TObject *Sender)
{
    Close();ModalResult = mrOk;
//    ModalResult = bCurrency? (int)m_pCurFormat : (int)m_pNumFormat;
}


void __fastcall TfrmFormatNombre::cbxChiffreGroupeKeyUp(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
    RefreshApercu(Sender);
}
//---------------------------------------------------------------------------


void __fastcall TfrmFormatNombre::cbxChiffreGroupeKeyPress(TObject *Sender,
      char &Key)
{
    if( Key >= '0' && Key <= '9')
        Key =' ';
}
//---------------------------------------------------------------------------

void __fastcall TfrmFormatNombre::btnParDefautClick(TObject *Sender)
{
    if( bCurrency)
    {
        if( m_pCurFormat)
        {
            delete m_pCurFormat;
            m_pCurFormat = NULL;
        }
        InitCurrency();
    }
    else
    {
        if( m_pNumFormat)
        {
            delete m_pNumFormat;
            m_pNumFormat = NULL;
        }
        InitNumber();
    }
    RefreshApercu(NULL);
}
//---------------------------------------------------------------------------

void __export __fastcall ChangeFormatCurrency( EICCurrencyFmt **pCurFormat, EICCurrencyFmt *pCurFormatParDefaut)
{
   // EICCurrencyFmt *c = (*pCurFormat)? *pCurFormat : pCurFormatParDefaut;
    TfrmFormatNombre* frmFormatNombre = new TfrmFormatNombre( Screen->ActiveForm, *pCurFormat, pCurFormatParDefaut);
    if( frmFormatNombre->ShowModal() == mrOk)
    {
        if( pCurFormatParDefaut)
        {
            if( *frmFormatNombre->CurFormat != *pCurFormatParDefaut)
                *pCurFormat = frmFormatNombre->CurFormat;
            else if( *pCurFormat)
            {
                delete frmFormatNombre->CurFormat;
                *pCurFormat = NULL;
            }
        }
        else
            *pCurFormat = frmFormatNombre->CurFormat;
    }
    delete frmFormatNombre;
}
//---------------------------------------------------------------------------

void __export __fastcall ChangeFormatNumber( EICNumberFmt **pNumFormat, EICNumberFmt *pNumFormatParDefault)
{
  //  EICNumberFmt *n = (*pNumFormat)? *pNumFormat : pNumFormatParDefault;
    TfrmFormatNombre* frmFormatNombre = new TfrmFormatNombre( Screen->ActiveForm, *pNumFormat, pNumFormatParDefault);
    if( frmFormatNombre->ShowModal() == mrOk)
    {
        if( pNumFormatParDefault)
        {
            if( *frmFormatNombre->NumFormat != *pNumFormatParDefault)
                *pNumFormat = frmFormatNombre->NumFormat;
            else if( *pNumFormat)
            {
                delete frmFormatNombre->NumFormat;
                *pNumFormat = NULL;
            }
        }
        else
            *pNumFormat = frmFormatNombre->NumFormat;
    }
    delete frmFormatNombre;
}
//---------------------------------------------------------------------------


